package com.ybxx.fees.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;

import com.ybxx.core.model.BaseModel;

@Entity
@Table(name="tbl_fees_watermeter_records")
public class WatermeterRecords extends BaseModel {
	@Id
	private String wmfcode;
	@ManyToOne(targetEntity=Records.class,fetch=FetchType.LAZY)
	@JoinColumn(name="frcode",referencedColumnName="frcode") 
	private Records records;
	private String thereadings;
	private String currentreading;
	private String walkquantity;
	private String amount;
	private String charge;
	private String receivable;
	@OneToMany(targetEntity=WatermeterRecordItem.class,cascade={CascadeType.ALL},mappedBy="watermeterRecords",fetch = FetchType.LAZY)
	private Set<WatermeterRecordItem> items=new HashSet<WatermeterRecordItem>();
	@Transient
	private List<WatermeterRecordItem> itemsList=new ArrayList<WatermeterRecordItem>();
	public String getWmfcode() {
		return wmfcode;
	}
	public void setWmfcode(String wmfcode) {
		this.wmfcode = wmfcode;
	}
	public Records getRecords() {
		return records;
	}
	public void setRecords(Records records) {
		this.records = records;
	}
	public String getThereadings() {
		return thereadings;
	}
	public void setThereadings(String thereadings) {
		if(StringUtils.isNotEmpty(thereadings)){
			this.thereadings = thereadings;
		}
	}
	public String getCurrentreading() {
		return currentreading;
	}
	public void setCurrentreading(String currentreading) {
		if(StringUtils.isNotEmpty(currentreading)){
			this.currentreading = currentreading;
		}
	}
	public String getWalkquantity() {
		return walkquantity;
	}
	public void setWalkquantity(String walkquantity) {
		if(StringUtils.isNotEmpty(walkquantity)){
			this.walkquantity = walkquantity;
		}
	}
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		if(StringUtils.isNotEmpty(amount)){
			this.amount = amount;
		}
	}
	public String getCharge() {
		return charge;
	}
	public void setCharge(String charge) {
		if(StringUtils.isNotEmpty(charge)){
			this.charge = charge;
		}
	}
	public String getReceivable() {
		return receivable;
	}
	public void setReceivable(String receivable) {
		if(StringUtils.isNotEmpty(receivable)){
			this.receivable = receivable;
		}
	}
	public Set<WatermeterRecordItem> getItems() {
		return items;
	}
	public void setItems(Set<WatermeterRecordItem> items) {
		this.items = items;
	}
	public List<WatermeterRecordItem> getItemsList() {
		if(itemsList==null || itemsList.size()<=0){
			this.itemsList.addAll(items);
		}else{
			this.items.addAll(itemsList);
		}
		if(this.itemsList!=null){
			Collections.sort(this.itemsList, new Comparator<WatermeterRecordItem>(){

				@Override
				public int compare(WatermeterRecordItem o1, WatermeterRecordItem o2) {
					// TODO Auto-generated method stub
					return o1.getPrice().compareTo(o2.getPrice());
				}});
		}
		return itemsList;
	}
	
	
}
